package com.sky.service.impl;

import com.sky.constant.IsDefaultConstant;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import jdk.vm.ci.meta.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;
    /**
     * 新增地址
     * @param addressBook
     */
    @Override
    public void add(AddressBook addressBook) {
        //获取当前登录用户的id并赋值
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        //设置状态为0
        addressBook.setIsDefault(0);
        addressBookMapper.insert(addressBook);
    }

    /**
     * 查询当前登录用户的所有地址信息
     * @return
     */
    @Override
    public List<AddressBook> list() {
        //实例化AddressBook对象
        AddressBook addressBook = new AddressBook();
        //设置用户id为当前登录用户
        addressBook.setUserId(BaseContext.getCurrentId());
        //查询数据库信息并返回
        List<AddressBook> addressBookList = addressBookMapper.select(addressBook);
        return addressBookList;
    }

    /**
     * 查询默认地址
     * @return
     */
    @Override
    public AddressBook getDefault() {
        //实例化AddressBook对象 并设置用户id和默认地址
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(IsDefaultConstant.isDefault);

        //创建一个数组实例
        List<AddressBook> addressBooks = new ArrayList<AddressBook>();
        //调用查询方法
        addressBooks = addressBookMapper.select(addressBook);

        //返回数组中的第一组数据
        return addressBooks.get(0);
    }


    /**
     * 根据id修改地址
     * @param addressBook
     */
    @Override
    public void updateById(AddressBook addressBook) {
        //设置用户id
        addressBook.setUserId(BaseContext.getCurrentId());

        addressBookMapper.updateById(addressBook);
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @Override
    public AddressBook getAddressBookById(Long id) {
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setId(id);

       AddressBook addressBooks = addressBookMapper.getAddressBookById(addressBook);
        return addressBooks;
    }

    /**
     * 根据id删除地址
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        //实例化地质对象，并设置id和用户id
        AddressBook addressBook = new AddressBook();
        addressBook.setId(id);
        addressBook.setUserId(BaseContext.getCurrentId());

        addressBookMapper.delete(addressBook);
    }

    /**
     * 设置默认地址
     * @param addressBook
     */
    @Override
    public void setDefault(AddressBook addressBook) {
        //1.设置当前登录用户的id
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(IsDefaultConstant.isDefault);

        //2.实例化一个对象，设置用户id和默认地址
        AddressBook addressBook1 = new AddressBook();
        addressBook1.setUserId(BaseContext.getCurrentId());
        addressBook1.setIsDefault(IsDefaultConstant.isDefault);

        //创建一个数组实例，用来接收默认地址
        List<AddressBook> addressBooks = new ArrayList<AddressBook>();

        //3.先查询地址簿中是否已经存在默认地址
        addressBooks = addressBookMapper.select(addressBook1);

        //3.1如果存在默认地址就取消默认地址，然后设置当前id的地址为默认地址
        if (!addressBooks.isEmpty() && addressBooks.size() > 0 ){
            //取消之前的默认地址
            AddressBook addressBook2 = addressBooks.get(0);
            addressBook2.setIsDefault(IsDefaultConstant.isNotDefault);
            addressBookMapper.updateById(addressBook2);
        }
        //设置当前id的地址为默认地址
        addressBookMapper.updateById(addressBook);


        //1、将当前用户的所有地址修改为非默认地址 update address_book set is_default = ? where user_id = ?
//        addressBook.setIsDefault(0);
//        addressBook.setUserId(BaseContext.getCurrentId());
//        addressBookMapper.updateIsDefaultByUserId(addressBook);

        //2、将当前地址改为默认地址 update address_book set is_default = ? where id = ?
//        addressBook.setIsDefault(1);
//        addressBookMapper.update(addressBook);
    }
}
